package com.zjhome.cloudnote.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 序列化工具
 * 
 * @author zjhome
 *
 */
public class SerializeUtil {
	/**
	 * 序列化
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static String serialize(Object object) throws IOException {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
		objectOutputStream.writeObject(object);
		
		String result = byteArrayOutputStream.toString("ISO-8859-1");	// toString("ISO-8859-1");
		
		objectOutputStream.close();
		byteArrayOutputStream.close();
		
		return result;
	}
	
	
	/**
	 * 反序列化
	 * 
	 * @param str
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static Object deserialize(String str) throws IOException, ClassNotFoundException {
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
		ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
		
		Object object = objectInputStream.readObject();
				
		objectInputStream.close();
		byteArrayInputStream.close();
		
		return object;
	}
	
}
